#include "xos_impl.h"
#include "../global/head.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{

    xos_impl::xos_impl()
    {
    }

    xos_impl::~xos_impl()
    {
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // create xos object
    // 
    int xos_impl::create( enumXosObj enumType, void ** ppv )
    {
        int nRet = 0;

        if( ppv )
        {
            *ppv = 0;
        }

        switch( enumType )
        {
        case XOS_OBJ_DIR_MONITOR:
            {
                dir_monitor_impl * p = m_dir_monitor_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_CRT:
            {
                crt_impl * p = m_crt_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_DYNAMIC:
            {
                dynamic_impl * p = m_dynamic_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_DIR:
            {
                dir_impl * p = m_path_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_FILE:
            {
                file_impl * p = m_file_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_MISC:
            {
                misc_impl * p = m_misc_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_SELECT:
            {
                select_impl * p = m_select_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_AIO:
            {
                aio_impl * p = m_aio_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_TCP:
            {
                tcp_impl * p = m_tcp_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_UDP:
            {
                udp_impl * p = m_udp_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_EVENT:
            {
                event_impl * p = m_event_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_MUTEX:
            {
                mutex_impl * p = m_mutex_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_LOCK:
            {
                lock_impl * p = m_lock_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_THREAD:
            {
                thread_impl * p = m_thread_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_ACCEPTOR:
            {
                heart_acceptor * p = m_acceptor_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_HEART:
            {
                heart_impl * p = m_heart_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_TIME:
            {
                time_impl * p = m_time_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;


        case XOS_OBJ_CALLBACK:
            {
                callback_impl * p = m_callback_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;

        case XOS_OBJ_BIG_BUF:
            {
                big_buf_impl * p = m_big_buf_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_BUF:
            {
                buf_impl * p = m_buf_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;
        case XOS_OBJ_MSG:
            {
                msg_impl * p = m_msg_pool.GetItem();
                p->init();
                *ppv = p;
            }
            break;

        default:
            {
                nRet = 1;
            }
            break;
        }

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int xos_impl::term()
    {
        int nRet = 0;

        m_dir_monitor_pool.ReleaseAll();

        // 
        // aio
        // 
        m_select_pool.ReleaseAll();
        m_aio_pool.ReleaseAll();

        // 
        // callback
        // 
        m_callback_pool.ReleaseAll();

        // 
        // socket
        // 
        m_udp_pool.ReleaseAll();
        m_tcp_pool.ReleaseAll();

        // 
        // file path
        // 
        m_path_pool.ReleaseAll();
        m_file_pool.ReleaseAll();

        // 
        // sync
        // 
        m_event_pool.ReleaseAll();
        m_mutex_pool.ReleaseAll();
        m_lock_pool.ReleaseAll();

        // 
        // misc
        // 
        m_misc_pool.ReleaseAll();

        // 
        // time
        // 
        m_heart_pool.ReleaseAll();
        m_time_pool.ReleaseAll();
        m_acceptor_pool.ReleaseAll();

        // 
        // crt
        // 
        m_crt_pool.ReleaseAll();

        // 
        // dynamic
        // 
        m_dynamic_pool.ReleaseAll();

        // 
        // thread
        // 
        m_thread_pool.ReleaseAll();

        // 
        // buf
        // 
        m_big_buf_pool.ReleaseAll();
        m_buf_pool.ReleaseAll();
        m_msg_pool.ReleaseAll();

        // 
        // xos impl
        // 
        g_pXosImpl = 0;

        return nRet;
    }

    // 
    // release xso interface
    // 
    int xos_impl::release()
    {
        int nRet = 0;
        delete this;
        return nRet;
    }

} // xos
